package com.ldxx.mustwinlottery.service;

import android.app.IntentService;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.ldxx.mustwinlottery.model.bean.IssueBean;

import io.realm.Realm;
import io.realm.RealmConfiguration;

import com.ldxx.mustwinlottery.app.Migration;

/**
 * Created by LDXX on 2015/12/1.
 * company Ltd
 * liaodongxiaoxiao@gmail.com
 */
public class InitDataService extends IntentService {
    private static final String TAG = "InitDataService";
    //private static final String ISSUE_URL = LotteryApplication.HOST + "getCurrentIssue.do";
    Realm realm;

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public InitDataService(String name) {
        super(name);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        RealmConfiguration config = new RealmConfiguration.Builder(getApplicationContext())
                .schemaVersion(1)
                .name("lottery.realm")
                .migration(new Migration())
                .build();
        realm = Realm.getInstance(config);
        return super.onStartCommand(intent, flags, startId);
    }

    public InitDataService() {
        super("init");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        IssueBean info = realm.where(IssueBean.class).equalTo("issue_type", "1").findFirst();
        //请求获取号码推荐

        if (info != null && !TextUtils.isEmpty(info.getIssue())) {
            //OKHttpPostUtils<Recommend> ri = OKHttpPostUtils.get(Recommend.class);
            //Recommend rm = ri.objectWithPost(AppUtils.getHost() + "/recommend/getRecommend.do", "issue", info.getIssue());
            //db.save(rm);
//                final Recommend rm = XXRetrofitUtils.getRetrofit().create(RecommendService.class)
//                        .getRecommendByIssue(info.getIssue()).execute().body();
//                realm.executeTransaction(new Realm.Transaction() {
//
//                    @Override
//                    public void execute(Realm realm) {
//                        realm.copyToRealm(rm.getData());
//                    }
//                });
        }

        //获取杀号
        try {
            if (info != null && !TextUtils.isEmpty(info.getIssue())) {
               /* final List<KillBallBean> list = XXRetrofitUtils.getRetrofit().create(KillBallService.class)
                        .getKills(info.getIssue()).execute().body();
                realm.executeTransaction(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(list);
                    }
                });*/
            }
        } catch (Exception e1) {
            Log.e(TAG, "issue: " + e1.getMessage(), e1);
        }
        try {
            //2.请求服务器，获取最近一期开奖情况
            /*final LotteryResultBean result = XXRetrofitUtils.getRetrofit().create(LotteryResultService.class)
                    .getLastResult().execute().body().getData();
            realm.executeTransaction(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    realm.copyToRealm(result);
                }
            });*/
        } catch (Exception e) {
            Log.e(TAG, "onHandleIntent: " + e.getMessage(), e);
        }

        //3.请求服务器，要开奖期次的推荐情况

    }
}
